module.exports = function ($scope, $modal, toaster, userinfo, getDate, $state, $resource) {
	
	console.log("%c 居游 %c 做地球上最具互联网性、创新性、专业有爱的团队。 \xa9 2015-%d", 'font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;font-size:64px;color:#00bbee;-webkit-text-fill-color:#00bbee;-webkit-text-stroke: 1px #00bbee;', "font-size:12px;color:#999999;", (new Date).getFullYear());

	//统计接口只查询两次（7日和30日数据各一次），将查询的数据暂存
	var formdata = {
		'is_all_data': false,
		'xaxisData': [],			//图表使用的横坐标时间轴日期数据（近30天日期值）
		'xaxisDataIndex': {},		//xaxisData中日期位置对应数据的位置
		'saleNum30': [],			//近30日销量数据（页面主动缓存，刷新页面后重置）
		'usedNum30': [],			//近30日使用量数据（页面主动缓存，刷新页面后重置）
		'income30': [],				//近30日交易额数据（页面主动缓存，刷新页面后重置）
		'profit30': []				//近30日收益数据（页面主动缓存，刷新页面后重置）
	};

	//设置图形曲线的数据loadChart1(销售曲线)、loadChart2(营收曲线)
	$scope.setChartDate = function(btnFlag, loadChart1, loadChart2){
		var range = btnFlag == 'a7' ? -7 : 0;
		if(loadChart1){
			loadChart1.showData = btnFlag;
			loadChart1.xaxis = formdata.xaxisData.slice(range);
			loadChart1.data[0] = formdata.saleNum30.slice(range);
			loadChart1.data[1] = formdata.usedNum30.slice(range);
		}
		if(loadChart2){
			loadChart2.showData = btnFlag;
			loadChart2.xaxis = formdata.xaxisData.slice(range);
			loadChart2.data[0] = formdata.income30.slice(range);
			loadChart2.data[1] = formdata.profit30.slice(range);
		}
	}

	//两个统计接口查询全部曲线数据，并将结果缓存
	$scope.getAllCount = function(day_num, loadChart1, loadChart2){
		var para = {
			'day_num':day_num
		}
		var btnFlag = 'a7';
		if(day_num == 2){
			formdata.is_all_data = true;
			btnFlag = 'a30';
		}
		$resource('/api/as/tc/ticketorder/getCountForHomeList', {}, {}).save(para, function(res){
			if(res.errcode === 0){
				res.data.forEach(element => {
					var day = element.sale_date.substring(5);
					var index = formdata.xaxisDataIndex[day];
					formdata.saleNum30[index] = element.all_num;
					formdata.income30[index] = element.income_price/100;
					formdata.profit30[index] = (element.income_price - element.cost_price)/100;
					if(index == 29){
						$scope.todayNum.all_sell = element.all_num;
						$scope.todayNum.othersell = element.other_sale_num;
						$scope.todayNum.profitmoney = formdata.profit30[29];
						$scope.todayNum.totalmoney = formdata.income30[29];
					}
				});
				//将处理好的数据添加到图形中
				$scope.setChartDate(btnFlag, loadChart1, loadChart2);
			}
		});
		$resource('/api/as/tc/ticketorder/getCountForHome2List', {}, {}).save(para, function(res){
			if(res.errcode === 0){
				res.data.forEach(element => {
					var day = element.sale_date.substring(5);
					var index = formdata.xaxisDataIndex[day];
					formdata.usedNum30[index] = element.used;
				});
				$scope.todayNum.used = formdata.usedNum30[29];
				//将处理好的数据添加到图形中
				$scope.setChartDate(btnFlag, loadChart1, loadChart2);
			}
		});
	}

	//页面7日和30日的曲线切换按钮
	$scope.changeChartDay = function(type, chart1, chart2){
		if(chart1.showData && type == chart1.showData){
			return;
		}
		if(chart2.showData && type == chart2.showData){
			return;
		}

		if(type == 'a30' && !formdata.is_all_data){
			$scope.getAllCount(2, chart1, chart2);
		} else {
			$scope.setChartDate(type, chart1, chart2);
		}
	}

	//查询近7日的产品销售情况和销售渠道
	$scope.sevenSaleInfo = function(){
		$resource('/api/as/tc/ticketorder/getCountForHome3List', {}, {}).save({}, function(res){
			if (res.errcode === 0) {
				if(res.data.length > 0){
					$scope.piechart2.data.aa = {'value': 0, 'name': '自营销售'};
					res.data.forEach(element => {
						if($scope.piechart1.data['sale' + element.sale_code]){
							$scope.piechart1.data['sale' + element.sale_code].value += element.num;
						} else {
							$scope.piechart1.data['sale' + element.sale_code] = {
								'value': element.num,
								'name': element.name
							}
						}
						element.down_company_code = element.self == '1'?'aa':element.down_company_code;
						if($scope.piechart2.data[element.down_company_code]){
							$scope.piechart2.data[element.down_company_code].value += element.num;
						} else {
							$scope.piechart2.data[element.down_company_code] = {
								'value': element.num,
								'name': element.down_company_name
							}
						}
					});
				} else {
					$scope.piechart2.data.aa = {'value': 0, 'name': '无产品销售'};
					$scope.piechart1.data.sale = {'value': 0, 'name': '无产品销售'}
				}
			}
		});
	}


//----------上面是统计接口-----下面是页面功能接口--------

	//根据参数查询对应的消息列表
	$scope.noticeList = function(result, msgType, msgTypeSon){
		var para = {
			'type': '0',
			'msgType': msgType
		}
		if(msgTypeSon){
			para.msgTypeSon = msgTypeSon;
		}
		$resource('/api/ac/sc/informaticaService/findInfomatica', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
				$scope[result] = res.data.results;
			}
		});
	}

	//查询企业通告列表
	$scope.loadCompanyNotice = function (){
		$resource('/api/as/tc/notice/list', {}, {}).get({}, function (res) {
			if (res.errcode === 0) {
				$scope.notices = res.data.results;
			} else {
				toaster.error({title: '', body: res.errmsg});
			}
		});
	};

	//查询机构的平台预存
	$scope.getPlatfromPrice = function(){
		$resource('/api/ac/pc/payBmAccountService/setPlatfromPrice', {}, {}).save({}, function(res){
			if(res.errcode === 0){
				$scope.balance = {
					'balance': res.data.balance * 0.01,
					'send_sms_price': res.data.send_sms_price * 0.01,
					'send_sms_flag': res.data.send_sms_flag,
					'supplier_company_code': 'SYS',
					'deposit_system': '0'
				};
			}
		});
	}
	

	init();
	function init(){
		var today = new Date();
		for(var i = 0; i < 30; i++){
			var day = today.getDate();
			var month = today.getMonth() + 1;
			formdata.xaxisData[29-i] = (month<10?'0'+month:month) + '-' + (day<10?'0'+day:day);
			formdata.xaxisDataIndex[formdata.xaxisData[29-i]] = 29-i;
			today.setDate(day - 1);
			formdata.saleNum30[i] = 0;
			formdata.usedNum30[i] = 0;
			formdata.income30[i] = 0;
			formdata.profit30[i] = 0;
		}
		$scope.todayNum = {
			'all_sell': 0,
			'othersell': 0,
			'used': 0,
			'profitmoney': 0,
			'totalmoney': 0
		};
		//销售数据图表配置信息
		$scope.chart1 = {
			'showData': 'a7',
			'linename': ["预定量(张)", "使用量(张)"],
			'data': [],
			'setting': {
				'tooltip': {'trigger':'axis'},
				'serietype': 'line',
				'boundaryGap': false
			}
		}
		//营收数据图表配置信息
		$scope.chart2 = {
			'showData': 'a7',
			'linename': ["交易额", "收益额"],
			'data': [],
			'setting': {
				'tooltip': {'trigger':'axis', 'axisPointer': {'type': 'shadow'} },
				'serietype': 'bar',
				'boundaryGap': true
			}
		}
		$scope.piechart1 = {'title': '近7日产品销量', 'data': {} }
		$scope.piechart2 = {'title': '近7日渠道销量', 'data': {} }
		$scope.getAllCount(1, $scope.chart1, $scope.chart2);
		$scope.sevenSaleInfo();
		$scope.noticeList('shelvesChange', 'notice', 'notice_selltype');
		$scope.noticeList('priceChange', 'notice', 'notice_pricechange');
		$scope.loadCompanyNotice();
		$scope.getPlatfromPrice();
	}


	//获取企业通告详情
	$scope.getNoticeInfo = function(notice){
		var modalInstance = $modal.open({
			template: require('../views/moreNotices.html'),
			controller: 'moreNotices',
			size: 'lg',
			resolve: {
				notice: function () {
					return notice;
				}
			}
		});
	}

	//用户信息
	userinfo().then(function (res) {
		$scope.userobj = res;
		if(res.company_sys_area != 'LW'){
			$scope.setMobilePass(res);
		}
		$scope.userobj.photo = $scope.userobj.photo || 'http://shangke2.oss-cn-qingdao.aliyuncs.com/app/aa/bb/touxiang.jpg';
		$scope.userobj.time = getDate(new Date());
	});

	//用户补充手机信息&&验证初始密码  2017年10月25日15:08:39
	$scope.setMobilePass = function(obj){
		var code = '1';
		$resource('/api/ac/sc/systemUserService/checkMobilePass', {}, {}).save({}, function(res){
			if (res.errcode === 0) {
				code = res.data;
			}
			
			if(obj.mobile && code == '0'){
				//已绑定手机号,并且初始密码已修改,不提示补充信息
			} else {
				var modalInstance = $modal.open({
					template: require('../views/setPassword.html'),
					controller: 'setPassword',
					resolve: {
						user: function () {
							return obj;
						},
						code: function () {
							return code;
						}
					}
				});
				modalInstance.result.then(function () {
					
				});
			}
		});
	}

	//跳转个人信息
	$scope.goToOwn = function(){
		$state.go('app.person_info');
	}

	//查询待审核分销商列表
	$scope.getDownCompany = function(){
		$resource('/api/as/sc/office/getDownCompanyList', {}, {}).save({}, function(res){
			if(res.errcode === 0){
				$scope.downCompanyList = res.data.results;
				$scope.allDownCompany = res.data.totalRecord;
				$scope.showDownCompany = true;
			} else if(res.errcode === 1002){
				$scope.showDownCompany = false;
			} else {
				toaster.error({title: '', body: '查询待审核分销商失败'});
			}
		});
	}
	$scope.getDownCompany();
	//退票审核列表
	$scope.backTicketList = function(){
		$resource('/api/ac/tc/ticketOrderBackService/getApplyBackTicketList', {}, {}).save({'ticket_state': '5'}, function(res){
			if(res.errcode === 0){
				$scope.backTicketNum = res.data.totalRecord;
				$scope.showBackTicket = true;
			} else {
				$scope.showBackTicket = false;
			}
		});
	}
	$scope.backTicketList();
	//查看机构是否开启自动退款
	$scope.backMoneyList1 = function(){
		$resource('/api/ac/tc/ticketOrderBackService/saveorgetStateInfo', {}, {}).save({}, function(res){
			if(res.errcode === 0){
				$scope.showBackMoney = res.data.back_state == 0;
				if($scope.showBackMoney){
					$scope.backMoneyList2();
				}
			}
		});
	}
	//退款审核列表
	$scope.backMoneyList2 = function(){
		$resource('/api/as/tc/ticketorderback/orderbackmylist', {}, {}).save({'from_app_id': 'juyou','back_state': '0'}, function(res){
			if(res.errcode === 0){
				$scope.backMoneyNum = res.data.totalRecord;
			} else {
				toaster.error({title: '', body: '查询待审核分销商失败'});
			}
		});
	}
	$scope.backMoneyList1();

	$scope.auditDown = function(){
		var modalInstance = $modal.open({
			template: require('../../13deposit/views/updateDownCompany.html'),
			controller: 'updateDownCompany',
			size: 'lg',
			resolve: {
				downCompanyList: function () {
					return $scope.downCompanyList;
				}
			}
		});
	}

	//设置平台余额预警提醒
	$scope.setBalanceInfo = function() {
		var modalInstance = $modal.open({
			template: require('../../13deposit/views/depositEdit.html'),
			controller: 'depositEdit',
			size: 'lg',
			resolve: {
				obj: function () {
					return $scope.balance;
				}
			}
		});
		modalInstance.result.then(function(result) {
			$scope.balance.send_sms_price = result.send_sms_price;
			$scope.balance.send_sms_flag = result.send_sms_flag;
		});
	}

	$scope.auditBackTicket = function(){
		$state.go('app.backTicketApply');
	}

	$scope.auditBackMoney = function(){
		$state.go('app.orderbackorder');
	}

	$scope.recharge = function(){
		$state.go('app.platform_deposit');
	}
	
	$scope.detail = function(){
		$state.go('app.transaction', {'deposit_system': '0'});
	}


};